﻿// REVIEWCONTENT
$(function () {

    var searchReviewContentsObject = {
        hotelId: "",
        pageSize: 10,
        pageIndex: 2
    };

    // init searchReviewContentsObject
    searchReviewContentsObject.hotelId = $('#hotelIdHidden').val();

    // check view more button
    function hideTheViewMoreButtonIfEndOfRecord(isEnd) {
        if (isEnd == '0') {
            $('.divReviews .click-to-view-more-reviewcontent').hide();
        } else {
            $('.divReviews .click-to-view-more-reviewcontent').show();
        }
    }

    function viewMoreData(completeAction, appendResult) {
        // reset
        if (appendResult == false) {
            $(".divReviews .divReviews-items-section").html('');
        }
        var items = "";
        var isEnd = false;
        var total = 0;

        // get data
        $.ajax({
            beforeSend: function () { $.mobile.showPageLoadingMsg(); }, //Show spinner
            complete: function () { $.mobile.hidePageLoadingMsg() }, //Hide spinner
            url: "/M/Mobile_v2/WebServices/WebServices.asmx/GetReviewContents",
            dataType: "json",
            type: "POST",
            contentType: "application/json; charset=utf-8",
            data: "{ 'hotelId': '" + searchReviewContentsObject.hotelId
            + "',  'PageIndex': '" + searchReviewContentsObject.pageIndex
            + "', 'Limit': '" + searchReviewContentsObject.pageSize + "' }"
        }).then(function (response) {
            // read result
            $.each(response, function (i, val) {
                isEnd = val.IsEnd;
                items = val.Items;
                total = val.Total;
            });

            // clear or append more result
            if (appendResult) {
                $(".divReviews .divReviews-items-section").append(items);
            } else {
                $(".divReviews .divReviews-items-section").html(items);
            }

            // check hide the view more button
            hideTheViewMoreButtonIfEndOfRecord(isEnd);

            // call complete func
            if (completeAction != null) {
                completeAction();
            }

            // next page
            searchReviewContentsObject.pageIndex++;
        });
    }

    $('.divReviews .click-to-view-more-reviewcontent').click(function () {
        viewMoreData(null, true);
    });

    // call at init	
    if ($(".divReviews #reviewContentPageCount").val() > 1)
        hideTheViewMoreButtonIfEndOfRecord(1);
    else
        hideTheViewMoreButtonIfEndOfRecord(0);
});

// PHOTO
$(function () {

    var searchPhotosObject = {
        hotelId: "",
        pageSize: 10,
        pageIndex: 2
    };

    // init searchReviewContentsObject
    searchPhotosObject.hotelId = $('#hotelIdHidden').val();

    // check view more button
    function hideTheViewMoreButtonIfEndOfRecord2(isEnd) {
        if (isEnd == '0') {
            $('.divPhotos .click-to-view-more-photo').hide();
        } else {
            $('.divPhotos .click-to-view-more-photo').show();
        }
    }

    function viewMoreData2(completeAction, appendResult) {
        // reset
        if (appendResult == false) {
            $(".divPhotos .repPhotos-items").html('');
        }
        var items = "";
        var isEnd = false;
        var total = 0;

        // get data
        $.ajax({
            beforeSend: function () { $.mobile.showPageLoadingMsg(); }, //Show spinner
            complete: function () { $.mobile.hidePageLoadingMsg() }, //Hide spinner
            url: "/M/Mobile_v2/WebServices/WebServices.asmx/GetPhotos",
            dataType: "json",
            type: "POST",
            contentType: "application/json; charset=utf-8",
            data: "{ 'hotelId': '" + searchPhotosObject.hotelId
            + "',  'HotelName': '" + $('#hotelNameHidden').val()
            + "',  'PageIndex': '" + searchPhotosObject.pageIndex
            + "', 'Limit': '" + searchPhotosObject.pageSize + "' }"
        }).then(function (response) {
            // read result
            $.each(response, function (i, val) {
                isEnd = val.IsEnd;
                items = val.Items;
                total = val.Total;
            });

            // clear or append more result
            if (appendResult) {
                $(".divPhotos .repPhotos-items").append(items);
            } else {
                $(".divPhotos .repPhotos-items").html(items);
            }

            // check hide the view more button
            hideTheViewMoreButtonIfEndOfRecord2(isEnd);

            // call complete func
            if (completeAction != null) {
                completeAction();
            }

            // next page
            searchPhotosObject.pageIndex++;

            // * enable lazy load
            //			$("img.lazy").lazyload({
            //			effect: "fadeIn"
            //			}).removeClass("lazy");

            // * stop lazy load
            $('img.lazy').each(function () {
                $(this).attr("src", $(this).attr("data-original"));
            }).removeClass("lazy");

        });
    }

    $('.divPhotos .click-to-view-more-photo').click(function () {
        viewMoreData2(null, true);
    });

    // call at init	
    if ($(".divPhotos #photosPageCount").val() > 1)
        hideTheViewMoreButtonIfEndOfRecord2(1);
    else
        hideTheViewMoreButtonIfEndOfRecord2(0);
});